<template>
  <el-dialog
    :title="!dataForm.horderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="活动id" prop="activityHid">
        <el-input
          v-model="dataForm.activityHid"
          placeholder="活动id"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品id" prop="goodsId">
        <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="dataForm.goodsName"
          placeholder="商品名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="收货地址" prop="horderAdd">
        <el-input
          v-model="dataForm.horderAdd"
          placeholder="收货地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品数量" prop="goodsCount">
        <el-input
          v-model="dataForm.goodsCount"
          placeholder="商品数量"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品原价" prop="goodsPrice">
        <el-input
          v-model="dataForm.goodsPrice"
          placeholder="商品原价"
        ></el-input>
      </el-form-item>
      <el-form-item label="已砍" prop="horderYprice">
        <el-input v-model="dataForm.horderYprice" placeholder="已砍"></el-input>
      </el-form-item>
      <el-form-item label="应支付价格" prop="horderPrice">
        <el-input
          v-model="dataForm.horderPrice"
          placeholder="应支付价格"
        ></el-input>
      </el-form-item>
      <el-form-item label="订单状态" prop="horderStatus">
        <el-input
          v-model="dataForm.horderStatus"
          placeholder="订单状态"
        ></el-input>
      </el-form-item>
      <el-form-item label="支付时间" prop="payDate">
        <el-input v-model="dataForm.payDate" placeholder="支付时间"></el-input>
      </el-form-item>
      <el-form-item label="订单创建时间" prop="createDate">
        <el-input
          v-model="dataForm.createDate"
          placeholder="订单创建时间"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        horderId: 0,
        activityHid: "",
        goodsId: "",
        goodsName: "",
        horderAdd: "",
        goodsCount: "",
        goodsPrice: "",
        horderYprice: "",
        horderPrice: "",
        horderStatus: "",
        payDate: "",
        createDate: "",
        userId: "",
      },
      dataRule: {
        activityHid: [
          { required: true, message: "活动id不能为空", trigger: "blur" },
        ],
        goodsId: [
          { required: true, message: "商品id不能为空", trigger: "blur" },
        ],
        goodsName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        horderAdd: [
          { required: true, message: "收货地址不能为空", trigger: "blur" },
        ],
        goodsCount: [
          { required: true, message: "商品数量不能为空", trigger: "blur" },
        ],
        goodsPrice: [
          { required: true, message: "商品原价不能为空", trigger: "blur" },
        ],
        horderYprice: [
          { required: true, message: "已砍不能为空", trigger: "blur" },
        ],
        horderPrice: [
          { required: true, message: "应支付价格不能为空", trigger: "blur" },
        ],
        horderStatus: [
          { required: true, message: "订单状态不能为空", trigger: "blur" },
        ],
        payDate: [
          { required: true, message: "支付时间不能为空", trigger: "blur" },
        ],
        createDate: [
          { required: true, message: "订单创建时间不能为空", trigger: "blur" },
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.horderId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.horderId) {
          this.$http({
            url: this.$http.adornUrl(
              `/kms/bargainorder/info/${this.dataForm.horderId}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.activityHid = data.bargainOrder.activityHid;
              this.dataForm.goodsId = data.bargainOrder.goodsId;
              this.dataForm.goodsName = data.bargainOrder.goodsName;
              this.dataForm.horderAdd = data.bargainOrder.horderAdd;
              this.dataForm.goodsCount = data.bargainOrder.goodsCount;
              this.dataForm.goodsPrice = data.bargainOrder.goodsPrice;
              this.dataForm.horderYprice = data.bargainOrder.horderYprice;
              this.dataForm.horderPrice = data.bargainOrder.horderPrice;
              this.dataForm.horderStatus = data.bargainOrder.horderStatus;
              this.dataForm.payDate = data.bargainOrder.payDate;
              this.dataForm.createDate = data.bargainOrder.createDate;
              this.dataForm.userId = data.bargainOrder.userId;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/kms/bargainorder/${!this.dataForm.horderId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              horderId: this.dataForm.horderId || undefined,
              activityHid: this.dataForm.activityHid,
              goodsId: this.dataForm.goodsId,
              goodsName: this.dataForm.goodsName,
              horderAdd: this.dataForm.horderAdd,
              goodsCount: this.dataForm.goodsCount,
              goodsPrice: this.dataForm.goodsPrice,
              horderYprice: this.dataForm.horderYprice,
              horderPrice: this.dataForm.horderPrice,
              horderStatus: this.dataForm.horderStatus,
              payDate: this.dataForm.payDate,
              createDate: this.dataForm.createDate,
              userId: this.dataForm.userId,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
